package com.annoncesautomobile.modele.util;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

public class POSTAnnonce extends AsyncTask<String, Integer, String>{

	private Requete request;

	public void setRequest(Requete r)
	{
		this.request = r;
	}
	static String convertStreamToString(java.io.InputStream is) {
		java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
		return s.hasNext() ? s.next() : "";
	}
	@Override
	protected String doInBackground(String... params) {
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(params[0]);
			MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();        
			multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
			Map<String,String> requestMapMode = this.request.getMapMode();
			for( Entry<String, String> aEntry : requestMapMode.entrySet())
			{
				if(aEntry.getKey().startsWith("file"))
				{
					multipartEntity.addPart("file", new FileBody(new File(aEntry.getValue())));
				}
				else
				{
					multipartEntity.addPart(aEntry.getKey(), new StringBody(aEntry.getValue(),ContentType.TEXT_PLAIN));
				}

			}
			post.setEntity(multipartEntity.build());
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			return convertStreamToString(entity.getContent());
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}



	/*
	public static void postFile(String fileName) throws Exception {
		// Based on: http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("uploadFile");

		MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();        
		multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);


		multipartEntity.addPart("file", new FileBody(new File(fileName)));
		post.setEntity(multipartEntity.build());
		HttpResponse response = client.execute(post);
		HttpEntity entity = response.getEntity();

		// response.getStatusLine();  // CONSIDER  Detect server complaints

		entity.consumeContent();
		client.getConnectionManager().shutdown(); 

	}
	 */

}

